package top.lywivan.array;

import top.lywivan.array.inf.List;

import javax.swing.plaf.PanelUI;

/**
 * @author :lyw.ivan
 * @date :Created in 2022/5/25
 * @description :
 * @version: 1.0
 */
public class ArrayList implements List {

    //定义存储数据的数组
    int[] elementData;
    //定义集合元素个数
    private int size;
    //定义数组默认长度
    private final int DEFAULT_CAPACITY=10;

    public ArrayList(){
        elementData = new int[DEFAULT_CAPACITY];
    }

    public ArrayList(int capacity){
        if (capacity>=0){
            elementData = new int[capacity];
        }else {
            throw new IllegalArgumentException("capacity error,capacity="+capacity);
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public int indexOf(int o) {
        for (int i = 0; i < size; i++) {
            if (elementData[i]==o){
                return i;
            }
        }
        return -1;
    }

    @Override
    public boolean contains(int e) {
        return this.indexOf(e)>=0;
    }

    @Override
    public boolean add(int e) {
        //确认容量大小
        this.ensureCapacity(this.size+1);
        elementData[size++]=e;
        return true;
    }

    private void ensureCapacity(int size) {
        if (size>this.elementData.length){
            this.grow(size);
        }
    }

    private void grow(int size) {
        int newSize = elementData.length + elementData.length >> 1;
        if (newSize<size){
            newSize=size;
        }
        //完成数组的替换
        this.copyOf(newSize);
    }

    private void copyOf(int newSize) {
        int[] newArray=new int[newSize];
        System.arraycopy(elementData,0,newArray,0,size);
        elementData=newArray;
    }

    @Override
    public void add(int index, int element) {
        //判断索引合法性
        this.rangeCheck(index);
        //判断数组大小
        this.ensureCapacity(size+1);
        //复制数组，后移一位
        System.arraycopy(elementData,index,elementData,index+1,size-index);
        //插入数据
        this.elementData[index]=element;
        size++;
    }

    private void rangeCheck(int index) {
        if (index<0||index>size){
            throw new IndexOutOfBoundsException("index out of bounds,index="+index+",size="+size);
        }
    }

    @Override
    public int set(int index, int element) {
        //判断索引合法性
        this.rangeCheck(index);
        int oldValue = this.elementData[index];
        //修改元素
        this.elementData[index]=element;
        return oldValue;
    }

    @Override
    public int get(int index) {
        //判断索引合法性
        this.rangeCheck(index);
        return this.elementData[index];
    }

    @Override
    public int remove(int index) {
        //判断索引合法性
        this.rangeCheck(index);
        int oldValue = this.elementData[index];
        //数组元素前移一位
        System.arraycopy(elementData,index+1,elementData,index,size-index-1);
        //最后一位置空
        this.elementData[size-1]=0;
        size--;
        return oldValue;
    }

    @Override
    public void clear() {
        for (int i = 0; i < elementData.length; i++) {
            this.elementData[i]=0;
        }
        this.size=0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        for (int i = 0; i < size; i++) {
            if (i!=size-1){
                sb.append(this.elementData[i]).append(",");
            }else {
                sb.append(this.elementData[i]);
            }
        }
        sb.append("]");
        return sb.toString();
    }
}
